package Servidor_Socket;

import java.io.IOException;
import java.net.InetAddress;
import java.sql.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

class Mapeo {

    private Connection con;

    public Mapeo() {
        try {
                con = Conexion.getInstance(AppConstantes.ADMIN, AppConstantes.ADMIN_PASS).getCon();
        } catch (IOException ex) {
            Logger.getLogger(Mapeo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Mapeo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Mapeo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void almacenarLogin(String hash, Date date, InetAddress inetAddress, String login) {
        try {
            CallableStatement stmt = con.prepareCall("{call almacenar_login(?,?,?,?)}");
            stmt.setString(1, hash);
            stmt.setString(2, date.toString());
            stmt.setString(3, inetAddress.toString());
            stmt.setString(4, login);
            stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(Mapeo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean hashAnteriores(String hash,String login) {
		boolean antiguo=true;
		try {
			CallableStatement cs = con.prepareCall("{call hash_antiguo(?,?)}");
			cs.setString(1, hash);
			cs.setString(2, login);
			ResultSet rs = cs.executeQuery();
			if (rs.next())
				if (rs.getInt(1) == 0)
					antiguo = false;
			cs.close();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Se produjo un error!!","ERROR SQL",JOptionPane.WARNING_MESSAGE);
			antiguo=false;
		}
		return antiguo;
	}

    public boolean loginExistente(String login) {
        boolean retorno=true;
        try {
            CallableStatement cs = con.prepareCall("{call loginExiste(?)}");
            cs.setString(1, login);
            ResultSet rs = cs.executeQuery();
            if (rs.next())
                if (rs.getInt(1) == 0)
                    retorno = false;
            cs.close();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"Se produjo un error!!","ERROR SQL",JOptionPane.WARNING_MESSAGE);
            retorno=false;
        }
        return retorno;
	}

	public String obtenerClave(String login) {
		String pwd = "";
		try {
			CallableStatement cs = con.prepareCall("{call obtenerClave(?)}");
			cs.setString(1, login);
			ResultSet rs = cs.executeQuery();
			if (rs.next())
				pwd = rs.getString(1);
			//System.out.println("Password en el servidor");
			//System.out.println(pwd);
			
			cs.close();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Se produjo un error!!","ERROR SQL",JOptionPane.WARNING_MESSAGE);
		}
		return pwd;
	}

	public void alterLogin(String login,boolean activar)
	{
		//System.out.println(activar);
		String stractivar = " ENABLE";
		if (!activar)
			stractivar = " DISABLE";
		try {
			Statement stmt = con.createStatement();
			stmt.executeUpdate("ALTER LOGIN "+"\""+login+"\""+stractivar);
			//System.out.println("Entre en el try del alter login");

			stmt.close();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Se produjo un error!!","ERROR SQL",JOptionPane.WARNING_MESSAGE);
		}
	}

}
